let express = require("express");  //cnpm install express --save
var cors = require('cors')



let app = new express();
app.use(cors())
//后台配置允许跨域访问
//*代表所有的路由
// app.all("*", function (req, res, next) {
//     res.header("Access-Control-Allow-Origin", "*");
//     res.header("Access-Control-Allow-Methods", "GET,POST");
//     res.header("Access-Control-Allow-Headers", "Content-Type");
//     next()
// })

app.get("/data", (req, res) => {
    res.send("haogu-data")
})

app.get("/data1", (req, res) => {
    setTimeout(function () {
        res.send("haogu-data1")
    }, 1000)
})

app.get("/data2", (req, res) => {
    res.send("haogu-data2")
})

app.get("/haogu1", (req, res) => {
    setTimeout(function () {
        res.send({
            username: "haogu"
        })
    }, 1000)
})
app.get("/haogu2", (req, res) => {
    setTimeout(function () {
        res.send("haogu2")
    }, 2000)
})
app.get("/haogu3", (req, res) => {
    setTimeout(function () {
        res.send("haogu3")
    }, 3000)
})

//axios
// get传值
//http://localhost:3000/axios?username=haogu
app.get("/axios", (req, res) => {
    //req.query {username:"haogu"}
    res.send(req.query.username)
})
// 动态路由
//http://localhost:3000/axios/100
app.get("/axios2/:id", (req, res) => {
    //req.params  {id:100}
    res.send(req.params.id)
})

app.listen(3000, () => {
    console.log("3000running");
})
